<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcCms.Web.ViewModels.BackOfficeModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    MvcCms -
    <%=ViewData["PageTitle"]%>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeaderContent" runat="server">
    <style type="text/css">
        label.error
        {
            float: none;
            color: red;
            padding-left: .5em;
            vertical-align: top;
        }
        #HTML
        {
            width:700px;
            height:150px;
        }
    </style>
    <script type="text/javascript" src="/scripts/MvcCmsImageManager.js"></script>
    <script type="text/javascript" src="/scripts/ckeditor/ckeditor.js"></script>
    <script src="/scripts/ckeditor.js" type="text/javascript"></script>
    <link href="/content/ckeditor.css" rel="stylesheet" type="text/css" />
<%
    if (!Model._Widget.IsForWeightedContent && string.IsNullOrEmpty(Model._Widget.ZoneName))
    {
%>    
    <script type="text/javascript">
        var editor;
        function changeEnter() {
            // If we already have an editor, let's destroy it first.
            if (editor)
                editor.destroy(true);

            // Create the editor again, with the appropriate settings.
            editor = CKEDITOR.replace('HTML',
		    {
		        enterMode: Number(1),
		        shiftEnterMode: Number(2),
		        height: "230"
		    });
            MvcCmsImages.SetupCKEditor(editor);
        }
        window.onload = changeEnter;
        //]]>
    </script>
<%
    }
%>    
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div class="contentpage-outerdiv">
<%
    var existingSettings = Model._Widget.WeightedContentsSettings.FirstOrDefault();
    string existingShowOnlyForRoles = "";
    bool hideIfAuthenticated = false;
    int weight = 0;
    if (existingSettings != null)
    {
        hideIfAuthenticated = existingSettings.HideIfAuthenticated;
        weight = existingSettings.Weight;
        foreach (var role in Model._Widget.WeightedContentsSettings.FirstOrDefault().AllowedRoles)
        {
            if (existingShowOnlyForRoles == "")
            {
                existingShowOnlyForRoles += role.LoweredRoleName;
            }
            else
            {
                existingShowOnlyForRoles += "," + role.LoweredRoleName;
            }
        }
    }
%>
    <span class="field-validation-error">&nbsp;<%=ViewData["message"].ToString()%></span>
    <% using (Html.BeginForm("EditWidget", "BackOffice", FormMethod.Post, new { id = "form1" }))
       {%>
    <%
        bool iscattype = false;
        if (Model._Widget.Category != null)
        {
            iscattype = true;
        }
        bool ispagetype = false;
        if (Model._Widget.Page != null)
        {
            ispagetype = true;
        }           
    %>
    <fieldset>
        <legend>Edit Widget</legend>
        <p>
            <label for="Title">
                Title:</label>
            <%= Html.TextBox("Title", Model._Widget.Title, new { @class = "required" })%>
            <%= Html.ValidationMessage("Title", "")%>
        </p>
        <div>
    <%
           if (!Model._Widget.IsForWeightedContent)
           {    
    %>
            <div>
                <table>
                <tr>
                    <td>
                        <label for="AllPages">
                            All Pages:</label>
                        <%= Html.RadioButton("allPagesValue", "All", Model._Widget.AllPages)%>
                    </td>
                    <td>
                        <label for="Category">
                            Category:</label>
                        <%= Html.RadioButton("allPagesValue", "Cat", iscattype)%><%= Html.DropDownList("CategoryID")%>
                    </td>
                    <td>
                        <label for="Page">
                            Page:</label>
                        <%= Html.RadioButton("allPagesValue", "Page", ispagetype)%><%= Html.DropDownList("ContentPageID")%>
                    </td>
                </tr>
                <tr>
                    <td colspan="3">
                        Choose either all pages or a single page or a single category for the widget to
                        display on
                    </td>
                </tr>
            </table>
                <p>
                    <label for="TargetDiv">
                        Target Div:</label>
                    <%= Html.TextBox("TargetDiv", Model._Widget.TargetDiv)%>
                </p>
                or:
                <p>
                    <label for="ZoneName">
                        Zone Name:</label>
                    <%= Html.TextBox("ZoneName", Model._Widget.ZoneName)%>
                </p>
            </div>

    <%
           }
           else
           {    
    %>
	        <div>
                <p>
                <label for="WeightedContentID">Weighted ContentID:</label>
                <%= Html.DropDownList("WeightedContentID")%>
                </p><p>
                <label for="HideIfAuthenticated">Hide If Authenticated:</label>
                <%=Html.CheckBox("HideIfAuthenticated", hideIfAuthenticated, new { onclick = "ShowOnlyRolesToggle();" })%>
                </p><p id="showOnlyForRolesContainer">
                <label for="ShowOnlyForRoles">ShowOnlyForRoles: (enter role names seperated by comma)</label>
                <%=Html.TextBox("AllowedRoles", existingShowOnlyForRoles)%>
                </p><p>                          
                <label for="weight">Weight</label>
                <%=Html.TextBox("Weight", weight)%>
                </p>
            </div>
    <%
           }
    %>
        </div>
        <p>
            <label for="Summary">
                Description:</label>
            <%= Html.TextArea("Description", Model._Widget.Description)%>
        </p>
        <p>
            <label for="HTML">
                HTML:</label>
            <%= Html.TextArea("HTML", Model._Widget.HTML, new { @class = "mceEditor" })%>
        </p>
        <p>
            <label for="Body">
                Active:</label>
            <%= Html.CheckBox("Active", Model._Widget.Active)%>
            <%= Html.ValidationMessage("Active", "")%>
        </p>
        <p>
            <input type="submit" value="Save" />
        </p>
        <%=Html.Hidden("WidgetID", Model._Widget.WidgetID) %>
    </fieldset>
    <% } %>
    <div>
        <%=Html.ActionLink("Back to List", "ManageWidgets") %>
    </div>
</div>     
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ScriptContent" runat="server">
    <script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $("#form1").validate();
        function ShowOnlyRolesToggle() {
            $("#showOnlyForRolesContainer").toggle();
        }
    </script>
</asp:Content>